package com.meishij.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.meishij.R;
import com.meishij.entry.MallEntry;

import java.util.List;

/**
 * Created by 肖荣 on 2016/10/12.
 */

public class MallBodyAdapter extends RecyclerView.Adapter<MallBodyAdapter.ViewHolder> {
    private Context context;
    private List<Object> list;
    private int[] layoutId;
    private MallViewPagerAdapter mallViewPagerAdapter;
    private MallCategoryAdapter mallCategoryAdapter;
    private MallEndListAdapter mallEndListAdapter;

    public MallBodyAdapter(Context context, List<Object> list, int[] layoutId) {
        this.context = context;
        this.list = list;
        this.layoutId = layoutId;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return layoutId[0];
        } else if (position == 1) {
            return layoutId[1];
        } else if (position == 2) {
            return layoutId[2];
        } else if (position == 3) {
            return layoutId[3];
        } else {
            return layoutId[4];
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, viewType, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MallBodyAdapter.ViewHolder holder, int position) {
        if (position == 0) {//第一个加载轮播图,循环的次数一定要遵循list的长度,因为数据可能改变
            if (holder.mall_top_viewpager.getAdapter() == null) {
                List<MallEntry.BuyIndexTopBean> buy_index_top = (List<MallEntry.BuyIndexTopBean>) list.get(0);
                for (MallEntry.BuyIndexTopBean buyIndexTopBean : buy_index_top) {
                    ImageView imageView = new ImageView(context);
                    imageView.setImageResource(R.drawable.mall_point_unchecked);
                    int point_padding = (int) context.getResources().getDimension(R.dimen.point_padding);
                    imageView.setPadding(point_padding, point_padding, point_padding, point_padding);
                    holder.mall_focus_points.addView(imageView);
                }
                mallViewPagerAdapter = new MallViewPagerAdapter(context, buy_index_top);
                holder.mall_top_viewpager.setAdapter(mallViewPagerAdapter);
                holder.mall_focus_point.post(new Runnable() {
                    @Override
                    public void run() {
                        holder.mall_focus_point.setVisibility(View.VISIBLE);
                        ViewCompat.setTranslationX(holder.mall_focus_point, holder.mall_focus_points.getLeft());
                    }
                });
                holder.mall_top_viewpager.addOnPageChangeListener(holder);
                holder.mall_top_viewpager.setCurrentItem(1);
                holder.handler.obtainMessage(0, 1, 0).sendToTarget();
            }
        } else if (position == 1) {//6个分类
            List<MallEntry.CategoryInfosBean> categoryInfosBeen = (List<MallEntry.CategoryInfosBean>) list.get(1);
            GridLayoutManager layoutManager = (GridLayoutManager) holder.mall_category_infos.getLayoutManager();
            layoutManager.setSpanCount(categoryInfosBeen.size());
            mallCategoryAdapter = new MallCategoryAdapter(context, categoryInfosBeen);
            holder.mall_category_infos.setAdapter(mallCategoryAdapter);
        } else if (position == 2) {//购买推荐
            List<MallEntry.AdTopBean.DataBean> dataBeen = ((MallEntry.AdTopBean) list.get(2)).getData();
            GridLayoutManager manager = (GridLayoutManager) holder.mall_ad_top.getLayoutManager();
            manager.setSpanCount(dataBeen.size());
            MallAdAdapter mallAdAdapter = new MallAdAdapter(context, dataBeen);
            holder.mall_ad_top.setAdapter(mallAdAdapter);
        } else if (position == 3) {//四张大图
            List<MallEntry.ZcBean> zcBeen = (List<MallEntry.ZcBean>) list.get(3);
            GridLayoutManager manager = (GridLayoutManager) holder.mall_zc.getLayoutManager();
            manager.setSpanCount(1);
            MallZcAdapter mallZcAdapter = new MallZcAdapter(context, zcBeen);
            holder.mall_zc.setAdapter(mallZcAdapter);
        } else {//最下面的值得买
            List<MallEntry.EndListBean> endListBeen = (List<MallEntry.EndListBean>) list.get(4);
            if(mallEndListAdapter==null){
                GridLayoutManager layoutManager = (GridLayoutManager) holder.mall_end_list.getLayoutManager();
                layoutManager.setSpanCount(2);
                mallEndListAdapter = new MallEndListAdapter(context, endListBeen);
                holder.mall_end_list.setAdapter(mallEndListAdapter);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public void remove(int index){
        list.remove(index);
        notifyItemRemoved(index);
    }
    public void addNewData(int index,Object object){
        list.add(index,object);
        notifyItemInserted(index);
    }
    public void addEndList(List<MallEntry.EndListBean> list){
        mallEndListAdapter.addAll(list);
    }
    public static class ViewHolder extends RecyclerView.ViewHolder implements Handler.Callback, ViewPager.OnPageChangeListener {

        private ViewPager mall_top_viewpager;
        private LinearLayout mall_focus_points;
        private ImageView mall_focus_point;
        private Handler handler = new Handler(this);
        private RecyclerView mall_category_infos;
        private RecyclerView mall_ad_top;
        private RecyclerView mall_zc;
        private RecyclerView mall_end_list;

        public ViewHolder(View itemView) {
            super(itemView);
            mall_top_viewpager = ((ViewPager) itemView.findViewById(R.id.mall_top_viewpager));
            mall_focus_points = ((LinearLayout) itemView.findViewById(R.id.mall_focus_points));
            mall_focus_point = ((ImageView) itemView.findViewById(R.id.mall_focus_point));

            mall_category_infos = ((RecyclerView) itemView.findViewById(R.id.mall_category_infos));

            mall_ad_top = ((RecyclerView) itemView.findViewById(R.id.mall_ad_top));

            mall_zc = ((RecyclerView) itemView.findViewById(R.id.mall_zc));

            mall_end_list = ((RecyclerView) itemView.findViewById(R.id.mall_end_list));
        }

        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 0:
                    int arg1 = message.arg1;
                    if (arg1 > mall_top_viewpager.getAdapter().getCount() - 2) {
                        arg1 = 1;
                    }
                    mall_top_viewpager.setCurrentItem(arg1, arg1 != 1);
                    Message message1 = handler.obtainMessage(0, ++arg1, 0);
                    handler.sendMessageDelayed(message1, 5000);
                    break;
            }
            return true;

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float value = mall_focus_points.getLeft() + (position - 1 + positionOffset) * mall_focus_point.getWidth();
            value = Math.max(value, mall_focus_points.getLeft());
            value = Math.min(value, mall_focus_points.getRight() - mall_focus_point.getWidth());
            ViewCompat.setTranslationX(mall_focus_point, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter pagerAdapter = mall_top_viewpager.getAdapter();
            if (position == 0) {
                mall_top_viewpager.setCurrentItem(pagerAdapter.getCount() - 2, false);
            } else if (position == pagerAdapter.getCount() - 1) {
                mall_top_viewpager.setCurrentItem(1, false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
